import logging, os, time
import settings

def log_file():
    # 创建写入文件的handler
    curt = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
    log_path = '%s/logs' % settings.config['log']['path'] if 'log' in settings.config else os.getcwd()
    os.makedirs(log_path, exist_ok = True)
    log_name = '%s/%s.log' % (log_path, curt)
    fh = logging.FileHandler(log_name, mode='w')
    fh.setLevel(logging.DEBUG) 

    # 定义日志文件输出格式
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    fh.setFormatter(formatter)

    return fh

def log_cmd():
    # 创建控制台handler
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO) 
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    ch.setFormatter(formatter)

    return ch

def set_logger():
    # 创建logger
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    # 将logger添加到handler里面
    logger.addHandler(log_file())
    logger.addHandler(log_cmd())